<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="80%"
    @open="handleDialogOpen()" v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <!--{{assessModel}}-->
    <el-form ref="assessForm" :inline="true" :model="assessModel" label-width="70%">
      <el-form-item label="姓名">
          <el-input v-model="assessModel.name"  @keyup.native="queryIdCard()"></el-input>
      </el-form-item>
      <el-form-item label="身份证号">
        <!--<el-input v-model="assessModel.idcard"></el-input>-->
          <el-select  v-model="assessModel.idcard"  @change="selectIdCard()">
              <el-option
              v-for="item in memberList"
              :key="item.id"
              :label="item.idcard"
              :value="item.idcard">
            </el-option>
          </el-select>
            <!--@select="handleSelect"-->
      </el-form-item>

      <!--<el-form-item label="年龄">
        <el-input v-model="assessModel.age"></el-input>
      </el-form-item>-->
    <el-tabs type="boder-card">
      <el-tab-pane>
        <span slot="label"><i class="el-icon-user"></i>患者信息</span>
        <el-form>
              <el-form-item label="家族史:">
          <!--<el-input v-model="archives.fromc.familyHistory"></el-input>-->
            <el-checkbox-group v-model="dearDiseaseHas">
              <el-checkbox v-for="item in dearDisease" :label="item.id" :key="item.id" >{{item.name}}</el-checkbox>
            </el-checkbox-group>
          </el-form-item><br>
          <el-form-item label="病史:">
          <!--<el-input v-model="archives.fromc.medicalHistory"></el-input>-->
            <el-checkbox-group v-model="myDiseaseHas">
              <el-checkbox v-for="item in myDisease" :label="item.id" :key="item.id" >{{item.name}}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>

            <!-- 饮食情况 -->
            <div class="questionDiv" v-for="(item) in questions1" :key="item.id">
              <!--<p>{{item.question}}</p>-->
              <!--<el-radio-group v-model="isStart" v-show="item.isRadio==0" prop="question">
                <el-radio border v-for="value in item.tAssessQuestionOptionList" :label="value.options" :key="value.id"></el-radio>
              </el-radio-group>-->
              <!--{{questions1}}-->
              <el-form-item :label="item.question" prop="selectOption" v-show="item.isRadio==0">
                <el-radio-group v-model="item.answers">
                  <el-radio border v-for="value in item.tAssessQuestionOptionList" :label="value.id" :value="value.id" :key="value.id">{{value.options}}</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item :label="item.question" prop="checkOptions" v-show="item.isRadio==1">
                <el-checkbox-group v-model="item.checkOptions">
                  <el-checkbox border  v-for="value in item.tAssessQuestionOptionList" :label="value.id" :value="value.id" :key="value.id">{{value.options}}</el-checkbox>
                </el-checkbox-group>
              </el-form-item>

            </div><!--{{questions}}-->


          </el-form>
      </el-tab-pane>
    <el-tab-pane>
      <span slot="label"><i class="el-icon-help"></i>体检报告</span>
      <template>
        <el-table
          :data="List"
          border
          style="width: 100%">
          <el-table-column
            prop="name"
            label="检查项名称">
          </el-table-column>
          <el-table-column
            prop="jg"
            label="结果">
          </el-table-column>
          <el-table-column
            prop="minck"
            label="最小参考范围">
          </el-table-column>
          <el-table-column
            prop="maxck"
            label="最大参考范围">
          </el-table-column>
        </el-table>
      </template>
    </el-tab-pane>
    </el-tabs>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('assessForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {queryEATQuestion,queryAllQuestion,updateAssessById,addTAssessById,queryIdCardByName} from '@/api/xy/assess'
import { queryAllTDisease,queryDiseaseByMember,updateDiseaseById,addDiseaseById } from '@/api/xsf/diseaseLibrary'

export default {
  name: 'assess-edit',
  data() {
    return {
      memberList:[],
      //疾病信息
      myDisease:[],
      dearDisease:[],
      myDiseaseHas:[],
      dearDiseaseHas:[],
      formInline: {
          user: '',
          region: ''
        },
      loading: false,
      quest:{},
      questions1:[],
      questions2:[],
      questionTotal:[],
      assessModel: {},
      dialogProps: {
        visible: false,
        action: '',
        title: ''
      },
      memberId:0,
    List:"",
    }
  },
  methods: {
    selectIdCard(){
        this.$forceUpdate();
      },
    queryIdCard(){
          //alert(1);
            if(this.assessModel.name=='undefined'||this.assessModel.name==''){

            }else{
              queryIdCardByName(this.assessModel.name).then((response)=>{

                this.memberList=response.data;

                this.assessModel.idcard=this.memberList[0].idcard;

                //alert(this.memberList[0].idcard);
              });

            }

      },
    //通过member查询疾病
        queryDiseaseByMember(id){
            queryDiseaseByMember(id).then((response)=>{
              var myDiseases=response.data;

              this.dearDiseaseHas=[];
              this.myDiseaseHas=[];
              //alert(this.dearDiseaseHas);
              for(var i=0;i<myDiseases.length;i++){
                if(myDiseases[i].who==0){
                    this.myDiseaseHas.push(myDiseases[i].diseaseLibraryLib);
                }else if(myDiseases[i].who==1){
                    this.dearDiseaseHas.push(myDiseases[i].diseaseLibraryLib);
                }
              }
              //this.$set(this.myDiseaseHas,this.myDiseaseHas);
              //this.$set(this.dearDiseaseHas,this.dearDiseaseHas);

            })
        },
    initmodel(){
      this.questionTotal=[];
      //this.loading = true;

      // 饮食
      //alert(this.memberId);
      queryEATQuestion(this.memberId)
      .then(response => {
      this.questions1 = response.data;
            for(var i=0;i<this.questions1.length;i++){
              if(this.questions1[i].isRadio==1){
                var answers=this.questions1[i].answers.split(",");
                var array=[];
                for(var k=0;k<answers.length;k++){
                    array.push(Number.parseInt(answers[k]));
                }
                this.$set(this.questions1[i],'checkOptions',array);
              }else{
                this.$set(this.questions1[i],'answers', Number.parseInt(this.questions1[i].answers)); //起作用
              }
            }
            this.questionTotal.push(this.questions1);
      })
      .catch(error => {
        this.outputError(error);
         this.loading = false;
      })

    },
    initQuestion(){
      this.questionTotal=[];
      //this.loading = true;

      // 饮食
      //alert(this.memberId);
      queryAllQuestion()
      .then(response => {
      this.questions1 = response.data;
            for(var i=0;i<this.questions1.length;i++){
              if(this.questions1[i].isRadio==1){
              /*  var answers=this.questions1[i].answers.split(",");
                var array=[];
                for(var k=0;k<answers.length;k++){
                    array.push(Number.parseInt(answers[k]));
                }*/
                this.$set(this.questions1[i],'checkOptions',[]);
              }else{
                this.$set(this.questions1[i],'answers', 0); //起作用
              }
            }
            this.questionTotal.push(this.questions1);
      })
      .catch(error => {
        this.outputError(error);
         this.loading = false;
      })

    },
    queryAllTDisease(){
      //初始化疾病信息
      queryAllTDisease().then((response)=>{
          this.myDisease=response.data;
          this.dearDisease=response.data;
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if(this.dialogProps.action === 'add') {
            this.doSaveAssess()
          } else {
            this.doUpdateAssess()
          }
        } else {
          return false
        }
      });
    },
    doSaveAssess(){
      this.dialogProps.visible = false
      addTAssessById(this.assessModel.idcard,this.questionTotal).then((response)=>{
          //添加疾病库的信息
          addDiseaseById(this.assessModel.idcard,this.myDiseaseHas,this.dearDiseaseHas).then((response)=>{

               this.$message({
                  message: '添加成功！',
                  type: 'success'
                })
            this.$emit('save-finished')

          })
      })
    },
    doUpdateAssess(){
      this.dialogProps.visible = false
      updateAssessById(this.assessModel.id,this.questionTotal).then((response)=>{
          //修改疾病库的信息
          updateDiseaseById(this.memberId,this.myDiseaseHas,this.dearDiseaseHas).then((response)=>{
               this.$message({
                  message: '编辑成功！',
                  type: 'success'
                })
            this.$emit('save-finished')

          })
      })
    },
    handleDialogClose() {
      this.dialogProps.visible = false
    },
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs['assessForm'].clearValidate()
      })
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message)
      this.loading = false
      this.$message({
        showClose: true,
        message: '出错了，请按F12查看浏览器日志。',
        type: 'error'
      })
    }
  },
  mounted: function() {
    this.$nextTick(() => {
      this.$on('openEditAssessDialog', function(assess,memberId) {
        this.assessModel = assess
        this.dialogProps.action = 'edit'
        this.dialogProps.title = '修改'
        this.dialogProps.visible = true
        //alert(memberId);
        this.memberId=memberId;
        //alert(memberId);
        this.queryDiseaseByMember(this.memberId);
        this.initmodel();
        this.queryIdCardByName();
        //console.log(assess);
      })
      this.$on('openAddAssessDialog', function() {
        this.dearDiseaseHas=[];
        this.myDiseaseHas=[];
        this.dialogProps.action = 'add'
        this.dialogProps.title = '添加'
        this.assessModel = {};


        this.dialogProps.visible = true
        this.initQuestion();

      })
      this.$on("smm",function (smm) {
        this.List=smm
      })
    })
    //alert(this.memberId);


  },
  created(){
        this.initmodel();

        this.queryAllTDisease();


  }

}
</script>

